package com.zw.yzk.kabu.presenter;

import android.support.v7.app.AppCompatActivity;

import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.base.BaseSubscriber;
import com.zw.yzk.kabu.data.network.entity.CosplayListEntity;
import com.zw.yzk.kabu.data.network.task.CollectTask;
import com.zw.yzk.kabu.data.network.task.CollectedCosTask;
import com.zw.yzk.kabu.data.network.task.PraiseTask;
import com.zw.yzk.kabu.domain.repository.UseCase;
import com.zw.yzk.kabu.management.LoadingDialogManager;
import com.zw.yzk.kabu.mapper.CosplayListMapper;
import com.zw.yzk.kabu.view.view.CollectedCosView;

import javax.inject.Inject;
import javax.inject.Named;

/**
 * Created by zhanwei on 2017/3/4.
 */

public class CollectedCosPresenter implements Presenter {

    private UseCase getCosList;
    private UseCase collectCosplay;
    private UseCase praiseCosplay;
    private AppCompatActivity activity;
    private CollectedCosView collectedCosView;
    private CosplayListMapper mapper;

    @Inject
    public CollectedCosPresenter(@Named("collectedCosUseCase") UseCase getCosList,
                                 @Named("collectUseCase") UseCase collectCosplay,
                                 @Named("praiseUseCase") UseCase praiseCosplay,
                                 AppCompatActivity activity, CosplayListMapper mapper) {
        this.getCosList = getCosList;
        this.activity = activity;
        this.mapper = mapper;
        this.collectCosplay = collectCosplay;
        this.praiseCosplay = praiseCosplay;
    }

    public void setView(CollectedCosView view) {
        this.collectedCosView = view;
    }

    public void getCosplayList(int start) {
        getCosList.execute(new GetCosplayListSubscriber(activity), new CollectedCosTask(start));
    }

    public void praise(int cosplayId, boolean praise) {
        praiseCosplay.execute(new PraiseCosplaySubscriber(activity), new PraiseTask(cosplayId, praise ? 1 : 0));
    }

    public void collect(int cosplayId, boolean collect) {
        collectCosplay.execute(new CollectCosplaySubscriber(activity), new CollectTask(cosplayId, collect ? 1 : 0));
    }

    @Override
    public void resume() {

    }

    @Override
    public void pause() {

    }

    @Override
    public void destroy() {
        getCosList.unSubscribe();
        collectCosplay.unSubscribe();
        praiseCosplay.unSubscribe();
    }

    private class GetCosplayListSubscriber extends BaseSubscriber<CosplayListEntity> {

        public GetCosplayListSubscriber(AppCompatActivity appCompatActivity) {
            super(appCompatActivity);
        }

        @Override
        public void onNext(CosplayListEntity cosplayListEntity) {
            super.onNext(cosplayListEntity);
            collectedCosView.getCosplayListSuccess(mapper.transfer(cosplayListEntity));
        }

        @Override
        public void onCompleted() {
            super.onCompleted();
            LoadingDialogManager.getInstance().dismissLoadingDialog();
        }

        @Override
        public void onError(Throwable e) {
            super.onError(e);
            collectedCosView.getCosplayListFail();
        }

        @Override
        public String addErrorMessage() {
            return activity.getResources().getString(R.string.get_cosplay_list_fail);
        }
    }

    private class CollectCosplaySubscriber extends BaseSubscriber<Boolean> {

        public CollectCosplaySubscriber(AppCompatActivity appCompatActivity) {
            super(appCompatActivity);
        }

        @Override
        public String addErrorMessage() {
            return "";
        }

    }

    private class PraiseCosplaySubscriber extends BaseSubscriber<Boolean> {

        public PraiseCosplaySubscriber(AppCompatActivity appCompatActivity) {
            super(appCompatActivity);
        }

        @Override
        public String addErrorMessage() {
            return "";
        }
    }
}
